use "${base}\gfs_cleaned_merged_database.dta", clear

rename * , lower
ren annual_weight1  weight

foreach x in  belonging	approve_govt	bodily_pain	capable	close_to	comfort_rel	connected_rel	content	control_worry	covid_death	critical	days_exercise	depressed	discriminated	donated	drinks	expect_good	expenses	feel_anxious	forgive	freedom	give_up	god_punish	grateful	group_not_rel	happy	health_prob	help_stranger	hope_future	interest	life_approach	life_balance	life_purpose	life_sat	lonely	loved_by_god	mental_health	peace	people_help	physical_hlth	promote_good	sat_live sat_relatnshp	say_in_govt	show_love	suffering	threat_life	trust_people	volunteered	wb_five	wb_today	worry_safety	worthwhile	income_feelings {
replace `x'=. if `x'==-98
replace `x'=. if `x'==98
replace `x'=. if `x'==99
}

foreach x in parents_12yrs svcs_12yrs svcs_father svcs_mother {
replace `x'=. if `x'==97
replace `x'=. if `x'==-98
replace `x'=. if `x'==98
replace `x'=. if `x'==99
}

*************
****Flourishing***
**********

gen thriving=1 if wb_fiveyrs>=8 & wb_today>=7
replace thriving=0 if wb_fiveyrs<8 | wb_today<7
replace thriving=. if wb_fiveyrs==. | wb_today==.

sum  belonging	approve_govt	bodily_pain	capable	close_to	comfort_rel	connected_rel	content	control_worry	covid_death	critical	days_exercise	depressed	discriminated	donated	drinks	expect_good	expenses	feel_anxious	forgive	freedom	give_up	god_punish	grateful	group_not_rel	happy	health_prob	help_stranger	hope_future	interest	life_approach	life_balance	life_purpose	life_sat	lonely	loved_by_god	mental_health	peace	people_help	physical_hlth	promote_good	sat_live sat_relatnshp	say_in_govt	show_love	suffering	threat_life	trust_people	volunteered	wb_five	wb_today	worry_safety	worthwhile	income_feelings 

tab critical, gen(critical)
tab loved_by_god, gen(loved_by_god)
tab life_approach, gen(life_approach)
tab god_punish, gen(god_punish)
tab comfort_rel, gen(comfort_rel)

foreach x in close_to covid_death donated help_stranger health_prob sat_live volunteered {
replace `x'=0 if `x'==2
}

foreach x in say_in_govt {
replace `x'=0 if `x'==2
replace `x'=0 if `x'==3
}

*54 items
factor belonging	approve_govt	bodily_pain	capable	close_to comfort_rel1 	connected_rel	content	control_worry	covid_death	critical1	days_exercise	depressed	discriminated	donated	drinks	expect_good	expenses	feel_anxious	forgive	freedom	give_up		grateful	group_not_rel	happy	health_prob	help_stranger	hope_future	interest	life_balance	life_purpose	life_sat	lonely	///
loved_by_god1  life_approach1  god_punish1  ///
mental_health	peace	people_help	physical_hlth	promote_good	sat_live sat_relatnshp	say_in_govt	show_love	suffering	threat_life	trust_people	volunteered	wb_five	wb_today	worry_safety	worthwhile	income_feelings [aw=weight], mineigen(1)
rotate, oblique oblimin(0)
esttab e(L)
predict fac1 fac2 fac3 fac4 

*exclude non-loaders <.4: approve_govt close_to covid_death days_exercise donated drinks forgive group_not_rel health_prob help_stranger life_balance peace sat_live say_in_govt trust_people volunteered

*****************
**reverse code negative loaders**
****************
foreach  i in income_feelings capable {
gen RE`i'=4 if `i'==1
replace RE`i'=3 if `i'==2
replace RE`i'=2 if `i'==3
replace RE`i'=1 if `i'==4
}

foreach  i in critical1 critical2 loved_by_god1 god_punish1 life_approach1 {
gen RE`i'=-1*`i'
}

global f1 "hope_future	content	grateful	promote_good	worthwhile	sat_relatnshp	expect_good	wb_fiveyrs	show_love	life_purpose	happy	freedom	give_up	life_sat	people_help	wb_today	mental_health	physical_hlth	belonging"
global f2 "feel_anxious	control_worry	depressed	suffering	interest	bodily_pain	threat_life" 
global f3 "comfort_rel1	life_approach1	loved_by_god1	critical1	god_punish1"
global f4 "expenses worry_safety REincome_feelings"

alpha $f1
alpha $f2

foreach x in  $f1 $f2 $f3 $f4  {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

cor fac1 $f1 [aw=weight]
cor fac2 fac1 $f2 [aw=weight]
cor fac3 fac1 fac2 $f3 [aw=weight]
cor fac4 fac3 fac1 fac2 $f4 [aw=weight]
cor thriving fac1 fac2 fac3 fac4  [aw=weight]

egen HOPE=rowmean(hope_future	content	grateful	promote_good	worthwhile	sat_relatnshp	expect_good	wb_fiveyrs	show_love	life_purpose	happy	freedom	give_up	life_sat	people_help	wb_today	mental_health	physical_hlth	belonging)
egen HAPPY=rowmean(feel_anxious	control_worry	depressed	suffering	interest	bodily_pain	threat_life)
egen ACCEPTED_BY_GOD=rowmean(comfort_rel1	life_approach1	loved_by_god1	critical1	god_punish1)
egen FINANCIAL=rowmean(expenses worry_safety REincome_feelings)

*********
**PARENT-CHILD RELATIONSHIP QUALITY
*******
desc father_relatn
label list FATHER_RELATN
label list FATHER_LOVED

foreach x in father_relatn mother_relatn father_loved mother_loved {
replace `x'=. if  `x'<1 |  `x'>=98
}

gen good_dad=4 if father_relatn==1
replace good_dad=3 if father_relatn==2
replace good_dad=2 if father_relatn==3
replace good_dad=1 if father_relatn==4
gen good_mom=4 if mother_relatn==1
replace good_mom=3 if mother_relatn==2
replace good_mom=2 if mother_relatn==3
replace good_mom=1 if mother_relatn==4

gen good_dad_alt=4 if father_relatn==1
replace good_dad_alt=3 if father_relatn==2
replace good_dad_alt=2 if father_relatn==3
replace good_dad_alt=1 if father_relatn==4
replace good_dad_alt=1 if father_relatn==97

gen good_mom_alt=4 if mother_relatn==1
replace good_mom_alt=3 if mother_relatn==2
replace good_mom_alt=2 if mother_relatn==3
replace good_mom_alt=1 if mother_relatn==4
replace good_mom_alt=1 if mother_relatn==97

tab father_loved, gen(flove)
ren flove1 flove_yes
ren flove2 flove_no
ren flove3 flove_no_dad

tab mother_loved, gen(mlove)
ren mlove1 mlove_yes
ren mlove2 mlove_no
ren mlove3 mlove_no_mom

label list FATHER_LOVED

gen relation_structure="2 Lived with mom but not dad" if father_loved==97 & (mother_loved==2 | mother_loved==1)
replace relation_structure="4 Did not live with mom or dad" if father_loved==97 & mother_loved==97
replace relation_structure="3 Lived with dad but not mom" if mother_loved==97 & (father_loved==2 | father_loved==1)
replace relation_structure="1 Lived with mom and dad" if (mother_loved==2 | mother_loved==1) & (father_loved==2 | father_loved==1)
replace relation_structure="1 Lived with mom and dad" if mother_loved<97 & father_loved<97

*check of consistency
tab relation_structure  if father_loved!=97 & mother_loved!=97
tab relation_structure if flove_no_dad==1 & mlove_no_mom==0
tab relation_structure if flove_no_dad==0 & mlove_no_mom==1
tab relation_structure if mother_loved<97 & father_loved<97

tab relation_structure, gen(living_structure)
encode relation_structure, gen(living_arrangement)
label var living_structure1 "Knew mom and dad"
label var living_structure2 "Knew mom but not dad"
label var living_structure3 "Knew dad but not mom"
label var living_structure4 "Did not know mom or dad"

gen not_outsider_fam=1 if outsider==2
replace not_outsider_fam=0 if outsider==1

gen not_abused=1 if abused==2
replace not_abused=0 if abused==1

gen was_abused=1 if abused==1
replace was_abused=0 if abused==2

**factor analysis
factor flove_yes  flove_no_dad mlove_yes mlove_no_mom good_dad good_mom not_abused not_outsider_fam  living_structure2 living_structure3 living_structure4  [aw=weight]
factor flove_yes   mlove_yes  good_dad good_mom not_abused not_outsider_fam  living_structure1 living_structure2 living_structure3 living_structure4  [aw=weight]
factor flove_yes   mlove_yes  good_dad good_mom not_outsider_fam  living_structure4 living_structure2 living_structure3  [aw=weight]
factor flove_yes   mlove_yes  good_dad good_mom not_outsider_fam  living_arrangement  [aw=weight]
*preferred model
factor flove_yes   mlove_yes  good_dad good_mom not_outsider_fam  living_structure1  not_abused  [aw=weight], mineigen(1)
esttab e(L)
alpha flove_yes  mlove_yes  good_dad good_mom  not_outsider_fam

*Evidence that it is better to have no relation with a parent ("does not apply") than a bad one
sum good_mom good_dad flove_yes  mlove_ye flove_no mlove_no flove_no_dad mlove_no_mom not_outsider was_abused [aw=weight]
bysort father_relatn: sum good_mom good_dad flove_yes flove_no mlove_no flove_no_dad mlove_no_mom mlove_ye not_outsider was_abused [aw=weight]
bysort mother_relatn: sum good_mom good_dad flove_yes flove_no mlove_no flove_no_dad mlove_no_mom mlove_ye not_outsider was_abused [aw=weight]

**Standardize to global mean, unadjusted by family structure (automatically gives lower score if parents are missing)
foreach x in  flove_yes  mlove_yes good_dad good_mom not_outsider_fam {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen Z`x'=(`x'-M`x')/S`x'
drop M`x' S`x'
}

foreach x in SZflove_yes SZmlove_yes SZgood_dad SZgood_mom SZnot_outsider_fam {
gen `x'=.
}

**Standardize based on relationship structure, so mean=0 for each relationship structure, avoids formulaic penalty
forval i = 1/4 {
foreach x in  flove_yes  mlove_yes good_dad good_mom not_outsider_fam {
sum `x' [aw=weight] if living_arrangement==`i'
gen M`x'=r(mean) if living_arrangement==`i'
gen S`x'=r(sd) if living_arrangement==`i'
replace SZ`x'=(`x'-M`x')/S`x' if living_arrangement==`i'
drop  M`x' S`x'
}
}

bysort relation_structure: sum flove_yes [aw=weight]

bysort relation_structure: sum flove_yes  mlove_yes good_dad good_mom not_outsider_fam
bysort relation_structure: sum Zflove_yes  Zmlove_yes Zgood_dad Zgood_mom Znot_outsider_fam
bysort relation_structure: sum SZflove_yes  SZmlove_yes SZgood_dad SZgood_mom SZnot_outsider_fam

egen PCRQ_UNADJ=rowmean(Zflove_yes  Zmlove_yes Zgood_dad Zgood_mom Znot_outsider_fam)
egen PCRQ=rowmean(SZflove_yes  SZmlove_yes SZgood_dad SZgood_mom SZnot_outsider_fam)

/*
**Check patterns for logical consistency
***PCRQ_ADJ should be mostly uncorrelated with living structure (only in so far as low mean scores cluster together) and controlling for PCRQ_ADJ should
retain negative relationship between parental absence and wellbeing

reg HOPE  living_structure2 living_structure3   living_structure4 [aw=weight]
reg HOPE  PCRQ living_structure2 living_structure3   living_structure4 [aw=weight]
reg HOPE  PCRQ_ADJ living_structure2 living_structure3   living_structure4 [aw=weight]
reg PCRQ living_structure2 living_structure3   living_structure4 [aw=weight]
reg PCRQ_ADJ living_structure2 living_structure3   living_structure4 [aw=weight]
*/

*********
**PARENT-RELIGIOSITY
*******

foreach x in  svcs_father svcs_mother {
gen RE`x'=4 if `x'==1
replace RE`x'=3 if `x'==2
replace RE`x'=2 if `x'==3
replace RE`x'=1 if `x'==4
}

gen nev_attend_dad=svcs_father
replace nev_attend_dad=. if svcs_father<0
replace nev_attend_dad=. if svcs_father>4

gen nev_attend_mom=svcs_mother
replace nev_attend_mom=. if svcs_mother<0
replace nev_attend_mom=. if svcs_mother>4

gen parent_relig=1 if rel1>=1 & rel1<=96
replace parent_relig=0 if rel1==97

factor parent_relig REsvcs_father REsvcs_mother [aw=weight]
esttab e(L)
alpha parent_relig REsvcs_father REsvcs_mother


**Standardize based on relationship structure, so mean=0 for each relationship structure, avoids formulaic penalty
foreach x in SZparent_relig SZREsvcs_father SZREsvcs_mother {
gen `x'=.
}

forval i = 1/4 {
foreach x in  parent_relig REsvcs_father REsvcs_mother {
sum `x' [aw=weight] if living_arrangement==`i'
gen M`x'=r(mean) if living_arrangement==`i'
gen S`x'=r(sd) if living_arrangement==`i'
replace SZ`x'=(`x'-M`x')/S`x' if living_arrangement==`i'
drop  M`x' S`x'
}
}

**Standardize to global mean, unadjusted by family structure (automatically gives lower score if parents are missing)
foreach x in  parent_relig REsvcs_father REsvcs_mother {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

egen parent_relig_index_unadj=rowmean(parent_relig REsvcs_father REsvcs_mother)
label var parent_relig_index_unadj "Index of parental religiosity unadjusted"
egen parent_relig_index=rowmean(SZparent_relig SZREsvcs_father SZREsvcs_mother)
label var parent_relig_index "Index of parental religiosity"

***
*Self-religion
***

gen self_not_relig=0 if rel2>=1 & rel2<=96
replace self_not_relig=1 if rel2==97

gen self_relig=1 if rel2>=1 & rel2<=96
replace self_relig=0 if rel2==97

gen relig_import=1 if rel_import==1
replace relig_import=0 if rel_import==2

gen attendance=1 if attend_svcs==5
replace attendance=2 if attend_svcs==4
replace attendance=3 if attend_svcs==3
replace attendance=4 if attend_svcs==2
replace attendance=5 if attend_svcs==1

factor relig_import attendance self_relig [aw=weight]
esttab e(L)
alpha relig_import attendance self_relig

foreach x in  relig_import attendance self_relig {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

egen self_relig_index=rowmean( relig_import attendance self_relig )
label var self_relig_index "Index of respondent religiosity"


**********
**other controls**
*********
desc education_3
label list EDUCATION_3
replace education_3=. if education_3<1 | education_3>3
tab education_3, gen(edu)
ren edu1 primary
ren edu2 secondary
ren edu3 tertiary

replace num_children=. if num_children<0 | num_children>97
egen children=cut(num_children),at(0,1,2,3,100)
tab children, gen(child)
ren child1  no_children
ren child2 one_child
ren child3 two_children
ren child4 three_plus_children

gen parents_married=1 if parents_12yrs==1
replace parents_married=0 if parents_12yrs>1 & parents_12yrs<=5

gen parent_died=0 if parents_12yrs>=1 & parents_12yrs<=5
replace parent_died=1 if parents_12yrs==4

gen child_poverty=income_12yrs
replace child_poverty=. if income_12yrs<1 | income_12yrs>4

gen female=1 if gender==2
replace female=0 if gender==1 | gender==3 | gender==4

gen male=1 if gender==1
replace male=0 if gender==2 | gender==3 | gender==4

gen other_gender=1 if gender==3 | gender==4
replace other_gender=0 if gender==1 | gender==2

gen employed=1 if employment==1  | employment==2
replace employed=0 if employment>2 & employment<=6

sum age
replace age=. if age>150
egen age_cat=cut(age), at(18,25,35,45,55,65,75,150)
tab age_cat, gen(age_group)
label var age_group1 "age 18-24"
label var age_group2 "age 25-34"
label var age_group3 "age 35-44"
label var age_group4 "age 45-54"
label var age_group5 "age 55-64"
label var age_group6 "age 65-74"
label var age_group7 "age 75 and older"


replace income=. if income>2413
replace income=. if income<0

bysort country: egen maxi=max(income)
bysort country: egen mini=min(income)
desc income
label list INCOME
sum income
gen hhinc=100*(income-mini)/(maxi-mini)
tab hhinc
gen lnhhinc=ln(hhinc)

gen live_alone=1 if num_household==1
replace live_alone=0 if num_household!=1
replace live_alone=. if num_household<0 | num_household>=97

gen foreign_born=1 if born_country==2
replace foreign_born=0 if born_country==1

label list MARITAL_STATUS
gen married=1 if marital_status==2
replace married=0 if marital_status!=2
replace married=. if marital_status>=98

gen never_married=1 if marital_status==1
replace never_married=0 if marital_status!=1
replace never_married=. if marital_status>=98

tab urban_rural
label list URBAN_RURAL
replace urban_rural=. if urban_rural<1 | urban_rural>4
tab urban_rural, gen(local)
ren local1 rural
ren local2 village
ren local3 city
ren local4 suburb

global demo "male other_gender secondary tertiary hhinc  age_group2 age_group3 age_group4 age_group5 age_group6 age_group7 foreign_born  employed never_married married live_alone no_children one_child two_children  rural village suburb"
global parent_demo "parents_married living_structure2 living_structure3 living_structure4  child_poverty was_abused"

*CAPI-computer assisted face-to-face interview
*CATI-computer assisted phone
*CAWI-web

gen web_mode=1 if mode_recruit==3
replace web_mode=0 if mode_recruit==1 | mode_recruit==2

label var web_mode "Web-based data collection" 
label var male "Men"
label var other_gender "Respondents with undisclosed gender"
label var primary "primary education"
label var secondary "secondary education"
label var	tertiary "tertiary education"
label var	hhinc	"household income-national rank"
label var	age_group1	"Ages 18-25"
label var	age_group2	"Ages 25-34"
label var	age_group3	"Ages 35-44"
label var	age_group4	"Ages 45-54"
label var	age_group5	"Ages 55-64"
label var	age_group6	"Ages 65-74"
label var	age_group7	"Ages 75 and older"
label var	foreign_born	"Born in another country"
label var	employed	"Currently working for pay"
label var	never_married	"Never married"
label var	married	"Married"
label var	live_alone	"Live alone"
label var	no_children	"Do not have children"
label var	one_child	"Has one child"
label var	two_children	"Has two children"
label var	three_plus_children	"Has three or more children"
label var	rural	"Live in rural area"
label var	village	"Live in village or small town"
label var	suburb	"Live in suburb of city"
label var parents_married "Parents were married at age 12" 
label var parent_died  "One or more parents died"
label var  flove_no_dad "Father was dead or unfamiliar"
label var  mlove_no_mom  "Mother was dead or unfamiliar"
label var  child_poverty "Level of financial difficulty while growing up"
label var  was_abused "Respondent was abused sexually or physically"
label var REincome_feelings "Living comfortably on present income 1-4 scale"

*************
***MERGE EXTERNAL DATABASES**
*******
decode country, gen(country_name)
replace country_name=strrtrim(country_name)
replace country_name=strltrim(country_name)

**External country-level data**
merge m:1 country_name using "{clean_data}\combined_external_data.dta"
drop if _merge==2
drop _merge

global country_level_controls "v2x_polyarchy zwvs_traditional zwps_index lngdp"
global country_valid "zmort_rate"

**Standardize indexes**
foreach x in HOPE HAPPY   FINANCIAL  ACCEPTED_BY_GOD parent_relig_index self_relig_index PCRQ {
sum `x' [aw=weight]
gen M`x'=r(mean)
gen S`x'=r(sd)
gen FIX`x'=(`x'-M`x')/S`x'
replace `x'=FIX`x'
drop FIX`x' M`x' S`x'
}

label var parent_relig_index "Parental religiosity index"
label var self_relig_index "Respondent religiosity index"
label var PCRQ "Parent-child relationship quality index"
label var PCRQ_UNADJ "Parent-child relationship quality index un-adjusted for family structure"
label var  HOPE "Flourishing index"
label var  HAPPY "Mental health index"
label var  FINANCIAL "Financial wellbeing index"
label var  ACCEPTED_BY_GOD "Religious beliefs index"

save "${clean_data}\gfs_cleaned_merged_database.dta", replace

